package simsimdev;

import java.io.File;

import simsim.core.Simulation;
import simsim.gui.charts.XYLineChart;
import simsimframework.impl.ChartImpl;
import umontreal.iro.lecuyer.stat.Tally;

public class SimSimChart extends ChartImpl{

	private String classdir = new File("").getAbsolutePath();
	private String outputFolder = classdir+"/temp/";
	
	// 1- AverageErrorRate  2- StandardDeviationRate
	private int type;
	private final String name;
	private String xAxisLabel;
	private String yAxisLabel;
	private boolean saved;
	private String s;
	private String fileName;
	
	public SimSimChart(int type, double framesPerSecond) {
		super();
		this.type = type;
		switch(type){
		case 1:
			this.name = "Average Error Rate";
			this.xAxisLabel = "Average Error";
			this.yAxisLabel = "time(s)";
			this.fileName = "AverageErrorRateChart.pdf";
			break;
		case 2:
			this.name = "Standard Deviation Rate";
			this.xAxisLabel = "Standard Deviation";
			this.yAxisLabel = "time(s)";
			this.fileName = "StandardDeviationRateChart.pdf";
			break;
		default: 
			this.name = null;
			break;
		}
		saved = false;
	}

	public XYLineChart execute(int i){
		s = "s"+i;
		final XYLineChart chart = new XYLineChart(name, this.framesPerSecond, xAxisLabel, yAxisLabel) ;
		chart.setYRange( false, 0, 260 ) ;
		chart.setSeriesLinesAndShapes(s, true, true) ;
		chart.chart().removeLegend() ;
		return chart;
	}
	
	public void update(Tally t, XYLineChart chart) {
		double value;
		switch(type){
		case 1:
			value = t.average();
			break;
		case 2:
			value = t.standardDeviation();
			break;
		default: 
			value = -1;
			break;
		}
		chart.getSeries(s).add( Simulation.currentTime(), value);
		if(value <= 0.0 && this.save.getValue() == 1 && !saved)
			try {
				System.out.println(outputFolder+fileName);
				chart.saveChartToPDF(outputFolder+fileName, 330, 330);
				saved = true;
			} catch (Exception e) {
				System.out.println("-> Gateway: (Erro) Impossivel gravar pdf.");
			}
	}
	
	public String getName() {
		return name;
	}

	public String getxAxisLabel() {
		return xAxisLabel;
	}

	public void setxAxisLabel(String xAxisLabel) {
		this.xAxisLabel = xAxisLabel;
	}

	public String getyAxisLabel() {
		return yAxisLabel;
	}

	public void setyAxisLabel(String yAxisLabel) {
		this.yAxisLabel = yAxisLabel;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
